package org.itboys.pay.weixin;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.utils.encryption.Digests;
import org.itboys.commons.utils.random.RandomUtils;

/**
 * 查询订单状态
 * @author huml
 *
 */
public class WeixinOrderQuery {
	
	public static final String URL = "https://api.mch.weixin.qq.com/pay/orderquery";
	/**SUCCESS—支付成功
		REFUND—转入退款
		NOTPAY—未支付
		CLOSED—已关闭
		REVOKED—已撤销（刷卡支付）
		USERPAYING--用户支付中
		PAYERROR--支付失败(其他原因，如银行返回失败)
	**/
	public static final String SUCCESS = "SUCCESS";
	public static final String REFUND = "REFUND";
	public static final String NOTPAY = "NOTPAY";
	public static final String CLOSED = "CLOSED";
	public static final String REVOKED = "REVOKED";
	public static final String USERPAYING = "USERPAYING";
	public static final String PAYERROR = "PAYERROR";

	private String appid;//公众账号ID
	private String mchId;//商户号
	private String transactionId;//微信订单id
	private String outTradeNo;//商户订单id   两个订单号只需传一个,二选一
	private String nonceStr;//随机字符串
	private String sign;//签名
	
	public WeixinOrderQuery(){}
	
	/**
	 * 实例化时完成签名
	 * @param appid
	 * @param mchId
	 * @param transactionId
	 * @param outTradeNo
	 * @param key
	 */
	public WeixinOrderQuery(String appid, String mchId, String transactionId, String outTradeNo, String key){
		this.appid = appid;
		this.mchId = mchId;
		this.transactionId = transactionId;
		this.outTradeNo = outTradeNo;
		this.nonceStr = RandomUtils.getClearRandomStr(16);
		String param = "appid="+appid+"&mch_id="+mchId+"&nonce_str="+nonceStr;
		if(StringUtils.isNotBlank(transactionId)){
			param += "&transaction_id="+transactionId;
		}
		if(StringUtils.isNotBlank(outTradeNo)){
			param += "&out_trade_no="+outTradeNo;
		}
		param += "&key=" + key;
		this.sign = Digests.md5(param).toUpperCase();
	}
	/**
	 * 获取xml格式的参数
	 * @return
	 */
	public String getXmlParam(){
		String xml = "<xml>"+
					   "<appid>"+this.appid+"</appid>"+
					   "<mch_id>"+this.mchId+"</mch_id>"+
					   "<nonce_str>"+this.nonceStr+"</nonce_str>";
		if(StringUtils.isNotBlank(this.transactionId)){
			xml += "<transaction_id>"+this.transactionId+"</transaction_id>";
		}
		if(StringUtils.isNotBlank(this.outTradeNo)){
			xml += "<out_trade_no>"+this.outTradeNo+"</out_trade_no>";
		}
		xml += "<sign>"+this.sign+"</sign></xml>";
		
		return xml;
	}
	
	public String getAppid() {
		return appid;
	}
	public void setAppid(String appid) {
		this.appid = appid;
	}
	public String getMchId() {
		return mchId;
	}
	public void setMchId(String mchId) {
		this.mchId = mchId;
	}
	public String getTransactionId() {
		return transactionId;
	}
	public void setTransactionId(String transactionId) {
		this.transactionId = transactionId;
	}
	public String getOutTradeNo() {
		return outTradeNo;
	}
	public void setOutTradeNo(String outTradeNo) {
		this.outTradeNo = outTradeNo;
	}
	public String getNonceStr() {
		return nonceStr;
	}
	public void setNonceStr(String nonceStr) {
		this.nonceStr = nonceStr;
	}
	public String getSign() {
		return sign;
	}
	public void setSign(String sign) {
		this.sign = sign;
	}
	
}
